package com.tr.syscms.controller;


import com.tr.common.util.Result;
import com.tr.common.util.ResultStatus;
import com.tr.syscms.entity.Menu;
import com.tr.syscms.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author xiijava
 * @since 2022-06-07
 */
//@CrossOrigin
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @GetMapping("queryButtonPermsByUserId")
    public Result queryButtonPermsByUserId(String token){
        return menuService.queryButtonPermsByUserId(token);
    }

    @GetMapping("queryTreeDataByUserId")
    public Result queryTreeDataByUserId(String userId){
        return menuService.queryTreeDataByUserId(userId);
    }

    /**
     * 添加角色权限关联方法
     * @param roleId
     * @param menuIds
     * @return
     */
    @PostMapping("addRoleAndMenu")
    public Result addRoleAndMenu(Integer roleId,String menuIds){
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.menuService.addRoleAndMenu(roleId,menuIds));
    }
    /**
     * 根据角色ID查询该角色对应的权限ID集合
     * @param roleId
     * @return
     */
    @GetMapping("queryMenuIdsByRoleId")
    public Result queryMenuIdsByRoleId(Integer roleId){
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.menuService.queryMenuIdsByRoleId(roleId));
    }
    /**
     * 查询所有菜单
     * @return
     */
    @GetMapping("menuTreeData")
    public Result menuTreeData(){
        return menuService.menuTreeData();
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Result selectOne(Integer id) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.menuService.queryById(id));
    }
    /**
     * 分页带参数查询
     *
     * @param map 参数
     * @return
     */
    @PostMapping("queryAll")
    public Result queryAll(@RequestBody Map map) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.menuService.queryAll(map));
    }
    /**
     * 添加
     *
     * @param menu
     * @return
     */
    @PutMapping("add")
    public Result add(@RequestBody Menu menu) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.menuService.insert(menu));
    }

    /**
     * 更新
     *
     * @param menu
     * @return
     */
    @PutMapping("update")
    public Result update(@RequestBody Menu menu) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.menuService.update(menu));
    }

    /**
     * 根据编号删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("deleteById")
    public Result deleteById(Integer id) {
        return new Result(ResultStatus.SUCCESS.getReturncode(),
                ResultStatus.SUCCESS.getReturnMessage(),
                this.menuService.deleteById(id));
    }
}

